package westh.ilib.struts.action.manage.answer;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOMessage;

import westh.util.YyAction;
import westh.ilib.bean.manage.AnswerBean;

public class AnswerListPreAction extends YyAction {
	//define commons log
	private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
			.getLog(AnswerListPreAction.class);

	public ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// check log and authority,don`s delete
		westh.log.YyLog.addFootprint(request, "动作-答案信息列表");
		/**
		 * ****************in data start********************************************
		 */
		// dbxml
		String dbName = "answer.dbxml";
		JSONObject inJson = new JSONObject();
		inJson.put("dbName", dbName);
		String inputStr = inJson.toString();
		/**
		 * ****************in data end********************************************
		 */
		String outString = "";
		try {
			MuleClient clientD = new MuleClient();
			// call WebService
			UMOMessage umo = clientD.send("axis:http://localhost:4381/LIBJson/iDlibJson?method=getAnswerList",inputStr, null);
			outString = (String) umo.getPayload();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		// analysis outString
		JSONObject picOutStr = JSONObject.fromObject(outString);
		String retCode = picOutStr.getString("retCode");
		List list = new ArrayList();
		AnswerBean answerBean = new AnswerBean();
		// judge result
		if (retCode.equals("0100")) {
			String result = picOutStr.getString("result");
			JSONArray resultArry = JSONArray.fromObject(result);
			int size = resultArry.size();
			for (int i = 0; i < size; i++) {
				answerBean = new AnswerBean();
				JSONObject jsonobj = JSONObject.fromObject(resultArry.get(i));
				answerBean.setAnswerId(jsonobj.getString("answerId"));
				answerBean.setQuestionId(jsonobj.getString("questionId"));
				answerBean.setAnswer(jsonobj.getString("answer"));
				answerBean.setAnswerer(jsonobj.getString("answerer"));
				answerBean.setAnswerTime(jsonobj.getString("answerTime"));
				answerBean.setAnswerRateAvg(jsonobj.getString("answerRateAvg"));
				list.add(answerBean);
			}
		}
		request.setAttribute("list", list);
		return mapping.findForward("succ");
	}
}
